package com.va.feedback.component;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.va.feedback.FeedbackItemConstants;

public class YesNoFeedback extends AbstractFeedback {

	private RadioGroup yesNoGroup;
	private RadioButton yesButton;
	private RadioButton noButton;
	private RadioButton noCommentsButton;

	/**
	 * Constructor for YesNoFeedback
	 * @param context context of the parent
	 */
	public YesNoFeedback(Context context) {
		super(context);
		
		addQuestionWidget();
		addAnswerComponent();
	}
	
	/**
	 * Sets the properties
	 */
	public void setComponentProperty(String type, Object value) {
		if (FeedbackItemConstants.PROPERTY_QUESTION.equals(type)) {
			String question = (String) value;
			questionWidget.setText(question);
		} else if (FeedbackItemConstants.PROPERTY_YES_BUTTON.equals(type)) {
			String yesText = (String) value;
			yesButton.setText(yesText);
		} else if (FeedbackItemConstants.PROPERTY_NO_BUTTON.equals(type)) {
			String noText = (String) value;
			noButton.setText(noText);
		} else if (FeedbackItemConstants.PROPERTY_NO_COMMENTS_BUTTON.equals(type)) {
			noCommentsButton.setVisibility(View.VISIBLE);
			String noCommentsText = (String) value;
			noCommentsButton.setText(noCommentsText);
		}
	}
	
	/**
	 * Adds the component which captures the user response to the question widget
	 */
	protected void addAnswerComponent() {
		yesNoGroup = new RadioGroup(context);
		RelativeLayout.LayoutParams yesNoGroupLayout = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		yesNoGroup.setId(2);
		yesNoGroupLayout.addRule(RelativeLayout.BELOW, 1);
		this.addView(yesNoGroup, yesNoGroupLayout);
		
		yesButton = new RadioButton(context);
		RelativeLayout.LayoutParams yesButtonLayout = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		yesButton.setText("Yes");
		yesNoGroup.addView(yesButton, 0, yesButtonLayout);
		
		noButton = new RadioButton(context);
		RelativeLayout.LayoutParams noButtonLayout = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		noButton.setText("No");
		yesNoGroup.addView(noButton, 1, noButtonLayout);

		noCommentsButton = new RadioButton(context);
		RelativeLayout.LayoutParams noCommentsButtonLayout = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		noCommentsButton.setText("No Comments");
		yesNoGroup.addView(noCommentsButton, 2, noCommentsButtonLayout);
		noCommentsButton.setVisibility(View.GONE);
	}

	/**
	 * Returns the response in the form of a JSONObject
	 * The response is : {'qid': qid, 'answer': either Yes or No}
	 */
	public JSONObject getFeedback() throws JSONException {
		int id = yesNoGroup.getCheckedRadioButtonId();
		if (id == RadioGroup.NO_ID)
			return null;
		RadioButton selectedButton = (RadioButton) yesNoGroup.findViewById(id);
		String answer = selectedButton.getText().toString();
		JSONObject object = new JSONObject();
		object.put("qid", qid);
		object.put("answer", answer);
		return object;
	}
	
}
